var do_App = sm("do_App");
var page = sm("do_Page");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var deviceone = require("deviceone");
var communityUrl = require("url/community");
var communityUpdateMessageUrl = communityUrl.url.message.update;
var communityToUpdateMessageUrl = communityUrl.url.message.toUpdate;
var communityJoinUrl = communityUrl.url.join.joinList;
var qqHttp = require("helper/http");
var imgTool = require("helper/picture");

// UI
var headerAout = ui("headerAout");
var aLayoutRoot = ui("aLayoutRoot");
var root = ui("$");
// 加载头部
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
// andriod返回键
page.on("back", function() {
	do_App.closePage();
});
// 左上角-返回
var headerLeft = ui(addheader + ".headerLeft");
// 右上角-
var headerRight = ui(addheader + ".headerRight");
var labelNext = ui(addheader + ".labelNext");
labelNext.visible = true;
headerLeft.on("touch", function() {
	do_App.closePage()
});
var header = ui("header");
var imgMax = 3;
header.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "编辑话题",
	"labelNext" : "确认"
})
header.refreshData();
/** **************处理-图片显示UI-end**************** */
var aLayoutImg = ui("aLayoutImg");
var aLayoutImgRoot = ui("aLayoutImgRoot");

var imgGridUI = aLayoutImgRoot.add("imgGridShower",
		"source://view/public/imgselect/imgGrid.ui", 20, 15);
var imgGridView = ui(imgGridUI + ".imgGrid");
var listdataImg = mm("do_ListData");
imgGridView.bindItems(listdataImg);
imgGridView.on("touch", function(data, e) {
	if(imgMax2<imgMax){
		imgMax2=imgMax2+1;
	}
	listdataImg.removeData([ data ]);
	imgGridView.refreshItems();
});
/** **************处理-图片显示UI-end**************** */
var imgMax2=3;
/** **************点击-添加图片-begin**************** */
aLayoutImg.on("touch", function(data, e) {
	if (listdataImg.getCount() >= imgMax) {
		nf.toast("不能多于" + imgMax + "张图片");
		return;
	}
	imgTool.imgMulti(imgMax2, selectImgBack);// 选择多张图片
});

function selectImgBack(imgList) {
	listdataImg.addData(imgList.map(function(path) {
		return {
			"imgPath" : path
		}
	}));
	imgGridView.refreshItems();
}
/** **************点击-添加图片-end**************** */
var layout_main = ui('do_LinearLayout_main');
var addboxContent = layout_main.add("boxContent",
		"source://view/reuse/form/mutiTextarea.ui", 0, 0);
var boxContent = ui("boxContent");
hashdata.addData({
	"textAreaHint" : '话题内容'
});
boxContent.bindData(hashdata);
boxContent.refreshData();
var textBoxContent = ui(addboxContent + ".textArea");

/*
 * **** 选择社区
 */
var labelCommunityId = '';
var addcommunityName = layout_main.add("communityName",
		"source://view/reuse/form/mutiLabel.ui", 0, 0);
var communityName = ui("communityName");
communityName.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "选择社区",
	"labelText" : '',
	"labelTipsVisible" : false,
	"labelTips" : ""
});
communityName.refreshData();
var communityNameText = ui(addcommunityName + ".labelText");
var addPicker = aLayoutRoot.add("picker",
		"source://view/reuse/form/oneCategory.ui", 0, 0);
var picker = ui("picker");
ui(addcommunityName + ".labelAout").on("touch", "", 300, function() {
	page.hideKeyboard();
	var params = {}
	var ptitle = {
		"title" : "选择社区",
		"id" : "cId"
	}
	qqHttp.post(communityJoinUrl, params, function(data) {
		var pcount = [ ptitle, data.communityList ];
		page.fire("oneCategoryList", pcount);
		page.fire("oneCategory", "true");
	});
});
/*
 * **** 选择类型
 */
var labelTypeId = '';
var addtypeName = layout_main.add("typeName",
		"source://view/reuse/form/mutiLabel.ui", 0, 0);
var typeName = ui("typeName");
typeName.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "选择类型",
	"labelText" : '',
	"labelTipsVisible" : false,
	"labelTips" : ""
});
typeName.refreshData();
var labelTypeName = ui(addtypeName + ".labelText");
ui(addtypeName + ".labelAout").on("touch", "", 300, function() {
	page.hideKeyboard();
	var param = {}
	var ptitle = {
		"title" : "选择类型",
		"id" : "tId"
	}
	var datas = [ {
		id : "1",
		name : "互助"
	}, {
		id : "2",
		name : "八卦"
	}, {
		id : "3",
		name : "活动"
	} ];
	var pcount = [ ptitle, datas ];
	page.fire("oneCategoryList", pcount);
	page.fire("oneCategory", "true");
});
page.on("refreshOneCategory", function(data) {
	if ("cId" == data[1]) {
		communityNameText.text = data[0].onename;
		labelCommunityId = data[0].oneid;
	} else if ("tId" == data[1]) {
		labelTypeName.text = data[0].onename;
		labelTypeId = data[0].oneid;
	}
});
var id;
// 页面装载完成后，开始初始化工作
page.on("loaded", function() {
	var data = page.getData();
	id = data.id;
	loadPage();
});
ui("do_ScrollView_1").redraw();
function loadPage() {
	var params = {
		id : id
	}
	qqHttp.post(communityToUpdateMessageUrl, params, function(data) {
		if (data != null) {
			textBoxContent.text = data.communityMessageVo.content;
			labelCommunityId = data.communityMessageVo.communityId;
			communityNameText.text = data.communityMessageVo.communityName;
			labelTypeId = data.communityMessageVo.type;
			labelTypeName.text = data.communityMessageVo.typeName;
			aLayoutRoot.tag = data.communityMessageVo.pathImgs;
			var imgs = data.communityMessageVo.pathImgs.split(";");
			imgMax2=imgMax2-imgs.length;
			var jsonImgs = [];
			for (var j = 0; j < imgs.length; j++) {
				jsonImgs.push({
					imgPath : imgs[j],
				});
			}
			listdataImg.addData(jsonImgs);
			imgGridView.refreshItems();
		}
	})
}
headerRight.on("touch", function() {
	update();
})
function update() {
	page.hideKeyboard();
	var imgs = aLayoutRoot.tag;
	if (labelTypeId == '' || labelTypeId == null) {
		nf.toast("亲，请选择类型。");
		return;
	}
	if (labelCommunityId == '' || labelCommunityId == null) {
		nf.toast("亲，请选择社区。");
		return;
	}
	if (textBoxContent.text.length<=0
			&& listdataImg.getCount() == 0) {
		nf.toast("亲，请写点什么吧，或者添加图片。");
		return;
	}
	if (listdataImg.getCount() == 0) {
		nf.toast("请添加图片");
		return;
	} else if (listdataImg.getCount() > imgMax) {
		nf.toast("不能多于" + imgMax + "张图片");
		return;
	}
	var imgFilesOld = [];
	var imgFilesNew = [];
	for (var i = 0; i < listdataImg.getCount(); i++) {
		var imgPath = listdataImg.getOne(i).imgPath;
		if (imgs.indexOf(imgPath) < 0) {// 不是原图片
			imgFilesNew.push(imgPath);
		} else {
			imgFilesOld.push(imgPath);
		}
	}
	if (imgFilesNew.length > 0) {
		qqHttp.form(imgFilesNew, "community/message", function(imgDatas) {
			var string4imgs = imgDatas.path;// 返回图片云端路径（后部份）用分号隔开
			imgFilesOld.push(string4imgs);
			var params = {
				"id" : id,
				"type" : labelTypeId,
				"img" : imgFilesOld.join(";"),
				"content" : textBoxContent.text,
				"communityId" : labelCommunityId
			};
			qqHttp.post(communityUpdateMessageUrl, params, function(data) {
				nf.toast("编辑成功");
				do_App.closePage();
			})
		});
	} else {
		var params = {
			"id" : id,
			"type" : labelTypeId,
			"img" : imgFilesOld.join(";"),
			"content" : textBoxContent.text,
			"communityId" : labelCommunityId
		};
		qqHttp.post(communityUpdateMessageUrl, params, function(data) {
			nf.toast("编辑成功");
			do_App.closePage();
		})
	}
}
//点击背景隐藏输入键盘
root.on("touch",function(){
	page.hideKeyboard();
})